package isquirrel.servlets;

import isquirrel.Logger;
import isquirrel.beans.Recommendation;
import isquirrel.beans.User;
import isquirrel.error.CannotLoginUserException;
import isquirrel.recommender.StaticDynamicRecommender;
import isquirrel.servlets.handler.HttpDBServlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.json.JSONException;

/**
 * Servlet implementation class Articles
 */
public class Articles extends HttpDBServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Articles() {
        super();
    }

	@Override
	protected void handleGet() throws ServletException, IOException,
			JSONException, CannotLoginUserException {	
	}

	@Override
	protected void handlePost() throws ServletException, IOException,
			JSONException, CannotLoginUserException {

		User u = getLoggedInUser();
		
		int latest = Integer.valueOf((String)request.getParameter("latest"));
		
		List<Recommendation> recommendations = null;
		if (latest == 0){
			recommendations = StaticDynamicRecommender.getRecommendations(u,false,5, false);
		}
		else{
			recommendations = StaticDynamicRecommender.getRecommendations(u,false,5, true);
		}
		
		if (recommendations.size() == 0){
			response.getWriter().println("<div>Article recommendations are being generated. Please wait a few seconds and refresh the page.</div>");
			return;
		}
		
		if (request.getParameter("viewmore") != null && ((String)request.getParameter("viewmore")).equals("t")){
			response.getWriter().println("<div id='rec_articles_content_changeable'>");
		}
		String style = "style='display:none' ";
		if (latest == 0){
			style = "";
		}
		response.getWriter().println("<div "+ style + "id='rec_articles_page_" + latest+ "'>");
		int i = 0;
		Logger.log(u, "viewing static/dynamic recs");
		for (Recommendation rec : recommendations) {
       		String desc = "";
			if (latest == 0){
				// we need to load up the first pagelet fast and use AJAX to populate the descriptions.
       			desc = "Loading description..";
			}
			else{
				// if pagelet > 0 then we just leave the user waiting (it shouldnt take long for updates after the first page
				// has been loaded) for title + description to be loaded instead of using ajax.
				desc = rec.getAbstract_short(75); //show 75 words of abstract text for each result
			}
			response.getWriter().print("<div class='articlecontainer'>" +
			     	"<div class='articledetails'>" + 
			       	   "<div class='articletitle'>" +
			       	   		rec.getPageTitle() +
			           "</div>" +
			           "<div id='abs_" + i + "page_0' class='articleabstract'>" +
			           		desc + 
			           "</div>" +
			        "</div>" +
			           "<a class='ilikebtn' onClick=\"like(0,this,'" + rec.getUrl() + "','" + rec.getPageTitle() + "',null,1)\"></a>"+ 
			           "<a id='url_" + i + "page_0' class='articleurl' target='_blank' href='" + rec.getUrl() + "'>Go to Wikipedia page</a>"+ 
			      "</div>");
			++i;
		}
		
		response.getWriter().println("</div>"); //close pagelet 
		if (request.getParameter("viewmore") != null && ((String)request.getParameter("viewmore")).equals("t")){
			response.getWriter().print("</div>"); //close content changeable -- this is shown only once
			response.getWriter().print("<div id='articles_viewmore' onClick='viewmore(\"Articles\")' class='viewmore clickable'> View more </div>");
		}
	}

}
